package com.netease.nie.service;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.constants.GoodsTypeStants;
import com.netease.nie.constants.OrdersTraceConstants;
import com.netease.nie.dao.CreditDao;
import com.netease.nie.dao.CreditHistoryDao;
import com.netease.nie.dao.ExchangeCodeDao;
import com.netease.nie.dao.ExchangeGoodsInfoDao;
import com.netease.nie.dao.OrdersAddressDao;
import com.netease.nie.dao.OrdersDao;
import com.netease.nie.dao.OrdersTraceDao;
import com.netease.nie.dto.GoodsExchangeParamDto;
import com.netease.nie.dto.OrdersAddressDto;
import com.netease.nie.dto.OrdersTraceDto;
import com.netease.nie.model.Orders;
import com.netease.nie.model.OrdersAddress;
import com.netease.nie.model.OrdersTrace;

/**
 * 商品兑换
 * @author wwbn3695
 * @createDate July 19th,2016
 *
 */
@Service
public class GoodsExchangeServiceImpl implements GoodsExchangeService {

	private static Logger logger = Logger.getLogger(GoodsExchangeServiceImpl.class);
	
	@Autowired
	private OrdersDao orderDao;
	
	@Autowired
	private CreditDao creditDao;
	
	@Autowired
	private CreditHistoryDao creditHistoryDao;
	
	@Autowired
	private ExchangeGoodsInfoDao exchangeGoodsInfoDao;
	
	@Autowired
	private ExchangeCodeDao exchangeCodeDao;
	
	@Autowired
	private OrdersTraceDao ordersTraceDao;
	
	@Autowired
	private OrdersAddressDao ordersAddressDao;
	
	@Transactional(rollbackFor = Exception.class)
	public Long updateGoodsExchangeStatus(Orders orders, GoodsExchangeParamDto goodsExchangeParamDto) throws Exception {
		Long orderId = null;
		if (orders.getExchangeCodeId() != null && orders.getExchangeCodeId() != 0) {
			//校验兑换码是否已经被使用
			logger.info("兑换码兑换开始");
			validateDuplicateOwnerId(orders.getExchangeCodeId());
			orderId = orderDao.insertOrder(orders);
			exchangeCodeDao.registerExchangeCodeOwner(orders.getExchangeCodeId(), orders.getUserId(), orders.getCreateTime(), orders.getCustomGoodsCode(), orders.getCustomGoodsId());
			exchangeGoodsInfoDao.deductGoodsStock(goodsExchangeParamDto.getExchangeGoodsConfigDto().getId());
			creditDao.deductUserCredit(goodsExchangeParamDto.getCredit());
			creditHistoryDao.insertCreditHistory(goodsExchangeParamDto.getCreditHistory());
			logger.info("兑换码兑换结束");
		} else if(orders.getGoodsType() == GoodsTypeStants.ENTITY.getCode()){
			logger.info("实物兑换开始");
			orderId = orderDao.insertOrder(orders);
			OrdersAddressDto ordersAddressDto = goodsExchangeParamDto.getOrdersAddressDto();
			OrdersAddress ordersAddress = new OrdersAddress();
			CopyUtils.copy(ordersAddress, ordersAddressDto);
			ordersAddress.setOrderId(orderId);
			ordersAddressDao.insertOrdersAddress(ordersAddress);
			OrdersTraceDto ordersTraceDto = goodsExchangeParamDto.getOrdersTraceDto();
			OrdersTrace ordersTrace = new OrdersTrace();
			CopyUtils.copy(ordersTrace, ordersTraceDto);
			ordersTrace.setOrderId(orderId);
			
			ordersTrace.setStatus(OrdersTraceConstants.GOT_GOODS.getCode());
			ordersTraceDao.insertOrdersTrace(ordersTrace); 
			
			ordersTrace.setStatus(OrdersTraceConstants.CONFIRM_ADDRESS.getCode());
			ordersTraceDao.insertOrdersTrace(ordersTrace);			//同时插入两张Trace表
			
			exchangeGoodsInfoDao.deductGoodsStock(goodsExchangeParamDto.getExchangeGoodsConfigDto().getId());
			creditDao.deductUserCredit(goodsExchangeParamDto.getCredit());
			creditHistoryDao.insertCreditHistory(goodsExchangeParamDto.getCreditHistory());
			logger.info("实物兑换结束");
		} else {
			logger.info("其他兑换开始");
			orderId = orderDao.insertOrder(orders);
			exchangeGoodsInfoDao.deductGoodsStock(goodsExchangeParamDto.getExchangeGoodsConfigDto().getId());
			creditDao.deductUserCredit(goodsExchangeParamDto.getCredit());
			creditHistoryDao.insertCreditHistory(goodsExchangeParamDto.getCreditHistory());
			logger.info("其他兑换结束");
		}
		return orderId;
	}
	
	//校验兑换码是否已经被使用
	private void validateDuplicateOwnerId(int exchangeCodeId) {
		Long ownerId = exchangeCodeDao.forceQueryOwnerIdById(exchangeCodeId);
		if (ownerId != null)
			throw new BusinessException(BusinessConstants.EXCHANGE_CODE_HAD_BEEN_USED.getCode(), "兑换码已被使用");
	}

}
